//添加编制车辆
var setnum =1;
var id ="{$Think.get.id}";
if(setnum == 1 ){
    $('.reduceSet').css('display','none');
}
$('.company_name').on('click',function(){
    opsearch(path.company.index+'?op=search')
})

// 循环遍历
$('.backs').on('click',()=>{
    console.log(123)
    window.location.href='{:url("html/car/car_compile")}'
})
Load(1)
{if $Think.get.id}
    Load();
	ajax(api.compile.list,{
		where_json:JSON.stringify({
			id:['in',"{$Think.get.id}"]
		})
	},'post').done(function(res) {
        console.log(res)
        Load(1);
		for(var key in res.data[0]){
            if(key == 'company'){
                $('input[name=company_id]').val(res.data[0]['company'].company_id)
                $('input[name=company_name]').val(res.data[0]['company'].company_name)
            }else if(key == 'strength_type'){
                $('select[name='+key+']').val(res.data[0][key])
            }else if(key == 'approval_document'){
                if(res.data[0][key]){
                    var imgData = JSON.parse(res.data[0][key])
                    if(imgData.approval_img){
                        dataImg.approval_img =imgData.approval_img
                        $('#img1').attr('src',domain+imgData.approval_img)
                    }else{
                        $('#img1').attr('src','/assets/img/noimg.gif')
                    }
                    if(imgData.budget_early_img){
                        dataImg.budget_early_img =imgData.budget_early_img
                        $('#img2').attr('src',domain+imgData.budget_early_img)
                    }else{
                        $('#img2').attr('src','/assets/img/noimg.gif')
                    }
                    if(imgData.budget_add_img){
                        dataImg.budget_add_img =imgData.budget_add_img
                        $('#img3').attr('src',domain+imgData.budget_add_img)
                    }else{
                        $('#img3').attr('src','/assets/img/noimg.gif')
                    }               
                }else{
                    $('#img1').attr('src','/assets/img/noimg.gif')
                    $('#img2').attr('src','/assets/img/noimg.gif')
                    $('#img3').attr('src','/assets/img/noimg.gif')
                }
            }else if(key =='standard'){
                let standardData= JSON.parse(res.data[0][key]);

                for(keys in standardData){
                    if(keys =='set'){
                for(var i =0;i<standardData[keys].length;i++){
                    if(i>0){
                       $('.set').after(setHtml) 
                    }

                }
                for(var i =0;i<standardData[keys].length;i++){
                  
                    $('.setname').eq(i).val(standardData[keys][i].setname)
                    $('.setuse').eq(i).val(standardData[keys][i].setuse)
                }
                    }else if(keys == 'new_energy'){
                        if(standardData[keys]==1){
                            $(`.${keys}`).attr('checked',true)
                        }else{
                            $(`.${keys}`).attr('checked',false)
                        }

                    }else{
                        $(`.${keys}`).val(standardData[keys])
                    }
                }

                for(keys in standardData){
                    if(keys == 'set'){                       
                    }else{
                        if(standardData.keys){
                            $(`.${keys}`).val(standardData[keys])
                        }else{
                            $(`.${keys}`).attr('placeholder','未填写')  
                        }
                        
                    }
                   
                }
                
            }else{
                if(res.data[0][key]){
                 $('input[name='+key+']').val(res.data[0][key])
                }else{
                    $('input[name='+key+']').attr('placeholder','未填写')                  
                }
            }
         
            form.render()
		}
	}).fail(function(err) {
		Err('请求失败')
	});
{/if}
// 上传图片
var dataImg= {};

upImage(api.upimage.upimage,'#test1', '#img1',dataImg,'approval_img');
upImage(api.upimage.upimage,'#test2', '#img2',dataImg,'budget_early_img');
upImage(api.upimage.upimage,'#test3', '#img3',dataImg,'budget_add_img');

var setHtml=`<div class="layui-form-item ">
<div class="layui-form-item">
    <div class="layui-inline">
        <label class="layui-form-label"></label>
        <div class="layui-input-inline" style="width: 100px;">
            <input type="text"  placeholder="设备名称" autocomplete="off" class="layui-input setname">
        </div>
        <div class="layui-form-mid">-</div>
        <div class="layui-input-inline" style="width: 100px;">
            <input type="text"  placeholder="用途" autocomplete="off" class="layui-input setuse">
        </div>
    </div>
</div>
</div> `;
$('.addSet').on('click',()=>{
    setnum += 1;
    if(setnum>1){
        $('.reduceSet').css('display','inline-block');
        console.log(setnum)
        $('.set').after(setHtml)
    }
})
$('.reduceSet').on('click',()=>{
    setnum -= 1;
    if(setnum==1){
        $('.reduceSet').css('display','none');
    }   
         $('.set').next().remove(); 
})
// 表单提交
form.on('submit(save)', function(data){
    console.log(dataImg)
    // Load()
	  console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
	  console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
      console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
      let approval_document =JSON.stringify({
        approval_img:dataImg.approval_img,
        budget_early_img:dataImg.budget_early_img,
        budget_add_img:dataImg.budget_add_img
                });
    data.field.approval_document =approval_document;
    let standard ={};
    standard.car_type= $('.car_type').val();
    standard.displacement =$('.displacement').val();
    standard.new_energy =$('.switch').val()=='on'?1:9;
    standard.price =$('.price').val();
    var set=[];
    for( var i = 0 ; i<$('.setname').length;i++){
        set.push({
            setname:$('.setname').eq(i).val(),
            setuse:$('.setuse').eq(i).val(),
        })
    }
    standard.set=set;
    data.field.standard =JSON.stringify(standard);
      {if $Think.get.id}
        data.field.id = id;
	 	ajax(api.compile.edit,data.field,'post').done(function(res) {
                console.log(res)
                Load(1)
                if(res.code==1){
                    Msg('编辑成功')
                    window.location.href='{:url("/html/car/car_compile")}'
                }else{
                    Err(res.msg)
                }
		}).fail(function(err) {
			Err('请求失败')
        });
        {else/}
        Load(1)
        ajax(api.compile.add,data.field,'post').done(function(res) {
            console.log(res)
            Load(1)
            if(res.code==1){
                Msg('添加成功')
                $('.keep').attr('disable',true)
                window.location.href='{:url("/html/car/car_compile")}'
            }else{
                Err(res.msg)
            }
    }).fail(function(err) {
        Err('请求失败')
    });
    {/if}
	  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
	});

// 返回上一页
$('.back').on('click',()=>{
    window.history.go(-1);
})